﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CIIS.Common;
using Contract.Domain;
using DevExpress.XtraLayout.Utils;
using DevExpress.XtraEditors;
using Framework.Domain;
namespace CIIS
{
    public partial class Reward : BaseForm
    {
        t_tq_tqjl tqjl
        {
            get { return BindEntity as t_tq_tqjl; }
        }

        public Reward()
        {
            InitializeComponent();

            normalSignatureSqly.InitControl("申请理由：", "使用民警：", "日期：", dao.SymjDcp);
            normalSignatureCbdwyj.InitControl("呈报单位意见：", "签名：", "日期：", dao.OfficeDcp);
            normalSignatureTgbmsh.InitControl("特管部门审核：", "签名：", "日期：", dao.OfficeDcp);
            normalSignatureDdldyj.InitControl("大队领导意见：", "签名：", "日期：", dao.UserDcp);
            normalSignatureLdsp.InitControl("领导审批：", "签名：", "日期：", dao.UserDcp);


            DevControlUtils.Instance.InitLookUpEdit(lookUpEditSqlb, dao.GetDropdownControlParam<bm_tq_tqkz_sqlb>());
            DevControlUtils.Instance.InitLookUpEdit(lookUpEditTbdw, dao.OfficeDcp);
            DevControlUtils.Instance.InitLookUpEdit(lookUpEditTqlb,
                new DropdownControlParam() { DisplayMember = "GNLB", ValueMember = "RYDH", DataSource = dao.GetListFromCache<t_tq_tqry>() });

            DevControlUtils.Instance.InitLookUpEdit(lookUpEditTqxm, dao.TqryDcp);
            lookUpEditTqxm.EditValueChanged += new EventHandler(lookUpEditTqxm_EditValueChanged);

            buttonSave.Click += new EventHandler(buttonSave_Click);
            buttonSave.DialogResult = DialogResult.OK;
            buttonBack.Click += new EventHandler(buttonBack_Click);

            dateEditSqsj.DateTime = DateTime.Today;
        }

        private void InitControls()
        {
            if (OptType == EOperateType.Detail)
            {
                layoutControlItemSave.Visibility = LayoutVisibility.Never;
            }
        }

        void buttonBack_Click(object sender, EventArgs e)
        {
            Close();
        }

        void lookUpEditTqxm_EditValueChanged(object sender, EventArgs e)
        {
            textEditTqdh.Text = lookUpEditTqxm.EditValue.ToString();
        }

        void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (OptType == EOperateType.Add)
                {
                    BindEntity.State.MarkNew();
                }
                else
                {
                    BindEntity.State.MarkDirty();
                }

                tqjl.SQSJ = dateEditSqsj.DateTime;

                tqjl.DDLD_YJ = normalSignatureDdldyj.Yj;
                tqjl.DDLD_QZ = normalSignatureDdldyj.Qz;
                tqjl.DDLD_SHSJ= normalSignatureDdldyj.Qzsj;

                tqjl.CBDW_YJ = normalSignatureCbdwyj.Yj;
                tqjl.CBDW_QZ = normalSignatureCbdwyj.Qz;
                tqjl.CBDW_CBSJ = normalSignatureCbdwyj.Qzsj;

                tqjl.SQLY = normalSignatureSqly.Yj;
                tqjl.SQLY_SYMJ = normalSignatureSqly.Qz;
                tqjl.SQLY_SYSJ = normalSignatureSqly.Qzsj;

                tqjl.SJLD_YJ = normalSignatureLdsp.Yj;
                tqjl.SJLD_QZ = normalSignatureLdsp.Qz;
                tqjl.SJLD_SPSJ = normalSignatureLdsp.Qzsj;

                tqjl.TGBM_YJ = normalSignatureTgbmsh.Yj;
                tqjl.TGBM_QZ = normalSignatureTgbmsh.Qz;
                tqjl.TGBM_SHSJ = normalSignatureTgbmsh.Qzsj;

                t_tq_tqjl o = dao.Dao.SaveOrUpdate(BindEntity) as t_tq_tqjl;

                XtraMessageBox.Show("保存成功");
            }
            catch
            {
                XtraMessageBox.Show("保存失败");
            }
            finally
            {
                Close();
            }
        }

        /// <summary>
        /// 绑定数据源，在show窗口之前需要调用
        /// </summary>
        /// <param name="tqry"></param>
        public override void BindData(Entity entity)
        {
            InitControls();

            BindEntity = entity as t_tq_tqjl;
            if (BindEntity == null)
            {
                BindEntity = new t_tq_tqjl();
                tqjl.State.MarkNew();
                tqjl.Id = Guid.NewGuid().ToString().Replace("-", string.Empty);
            }

            if (tqjl != null)
            {
                textEditSqje.DataBindings.Add("Text", tqjl, "SQJE");
                textEditTqdh.DataBindings.Add("Text", tqjl, "RYDH");

                lookUpEditSqlb.DataBindings.Add("EditValue", tqjl, "SQLB");
                lookUpEditTbdw.DataBindings.Add("EditValue", tqjl, "DJDW");
                lookUpEditTqlb.DataBindings.Add("EditValue", tqjl, "RYDH");     //从人员代号的方面绑定
                lookUpEditTqxm.DataBindings.Add("EditValue", tqjl, "RYDH");

                if (tqjl.SQSJ.HasValue)
                {
                    dateEditSqsj.DateTime = tqjl.SQSJ.Value;
                }

                normalSignatureSqly.SetValues(new object[] { tqjl.SQLY, tqjl.SQLY_SYMJ, tqjl.SQLY_SYSJ });
                normalSignatureDdldyj.SetValues(new object[] { tqjl.DDLD_YJ, tqjl.DDLD_QZ, tqjl.DDLD_SHSJ });
                normalSignatureCbdwyj.SetValues(new object[] { tqjl.CBDW_YJ, tqjl.CBDW_QZ, tqjl.CBDW_CBSJ });
                normalSignatureLdsp.SetValues(new object[] { tqjl.SJLD_YJ, tqjl.SJLD_QZ, tqjl.SJLD_SPSJ });
                normalSignatureTgbmsh.SetValues(new object[] { tqjl.TGBM_YJ, tqjl.TGBM_QZ, tqjl.TGBM_SHSJ });

            }
        }
    }
}
